 package me.freesoft.distributioin;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 获取 平均数和偏差
 * @author ma
 *
 */
public class GetAverageandStandardDevition {
	private List array = new ArrayList();
	
	private int num =0;
	
	
	
	
	public GetAverageandStandardDevition( List pe ) {
		 if(array.size()>0){
			 array.clear();
		 }
		 //System.out.println(pe);
		 
		 this.array =pe ;
		 num = array.size();
	}

	// 获取平均值
	public double getAverage(){
		double avg = 0;
		for(int i=0;i<num;i++){
			avg +=(double)array.get(i);
		}
		//System.out.println("平均数："+ins(avg/num));
		
		return ins(avg/num);
	}
	
	//获取标准差
	public double getStandardDevition(){
		double sum =0;
		for (int i = 0; i < num; i++) {
			//sum+=((double)array.get(i)-getAverage())*((double)array.get(i)-getAverage());
			
			sum+=Math.pow(((double)array.get(i)-getAverage()), 2); 
		}
		
		//System.out.println("标准差："+ ins(Math.sqrt(sum/(num-(double)1.0d)))  );
		return ins(Math.sqrt(sum/(num-(double)1.0d))) ;
		
		
	}
	
	public double ins (double u){
		BigDecimal   b   =   new   BigDecimal(u);  
		double   f1   =   b.setScale(4,   BigDecimal.ROUND_HALF_UP).doubleValue(); 
		
		return f1;
		
	}
	
}
